#include <stdio.h>
#include <algorithm>

#include <queue>
using namespace std;

int n;
int quals[1010];
int m;

int c;
int last;
int cost;

class app {
	public:
	int a, b, c;
};

app  appl[10010];

bool sortWay(app a, app b) {		
	return a.c<b.c;
}

bool super[1010];

int main() {
	scanf("%d", &n);

	for (int i=0; i<n; i++) 
		scanf("%d", &quals[i]);

	scanf("%d", &m);

	for (int i=0; i<m; i++) {
		scanf("%d %d %d", &appl[i].a, &appl[i].b, &appl[i].c);
		appl[i].a--;
		appl[i].b--;
	}
	
	sort(appl, appl+m, sortWay);

	c=0;
	last=0;
	cost=0;

	for (int i=0; i<n; i++) {
		super[i]=false;
	}

	for (int i=0; i<m; i++) {
		if (super[appl[i].b])
			continue;

		super[appl[i].b]=true;
		cost+=appl[i].c;
	}

	for (int i=0; i<n; i++) {
		if (!super[i])
			c++;
	}
	
	if (c>1) 
		printf("-1\n");
	else
		printf("%d\n", cost);

	return 0;
}
